<?
// TODO: require less to speed up PHP parsing
require_once 'forward.php';

if (isset($_POST['username']) && isset($_POST['password'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $user = auth($username, $password);
    if ($user !== false) {
        session_start();
        $_SESSION['user'] = $user;
    }
    header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
    exit;
}

if (isset($_GET['logout'])) {
    session_start();
    session_destroy();
    header("Location: http://".$_SERVER['HTTP_HOST']."/n/");
    exit;
}

$session_name = session_name();
if (isset($_COOKIE[$session_name])) {
    session_start();
    setcookie($session_name, $_COOKIE[$session_name], time() + 60*60*24*365);
}

if (isset($_SESSION['user'])) return;

render_begin(' :: log in');
?>
<div id="signuptip">
<a href="signup">sign up</a>
</div>
<?php
render_page_begin();
?>
<div class="centered">
<div id="login">
<form method="POST">
<input class="username" type="text" name="username" tabindex="1"/><br/>
<input class="password" type="password" name="password" tabindex="2"/><br/>
<input class="loginbtn" type="submit" value="Log in" tabindex="3"/>
</form>
</div>
</div>
<?php
render_page_end();
render_end();
exit;
?>
